import wmi
import os
import urllib3
import platform
w = wmi.WMI()
http = urllib3.PoolManager()
global list
list=[]
def info():
    display =""
    yingpan = ""
    ip = ""
    name = ""
    neicun =""
    for BIOSs in w.Win32_ComputerSystem():
        list.append(BIOSs.Caption)
        list.append(BIOSs.UserName)
        # list.append("电脑名称: %s" %BIOSs.Caption)
        # list.append("使 用 者: %s" %BIOSs.UserName)
    for BIOS in w.Win32_BIOS():
        list.append(BIOS.Description)
        list.append(BIOS.SerialNumber)
        # list.append("使用日期: %s" %BIOS.Description)
        # list.append("主板型号: %s" %BIOS.SerialNumber)
    for processor in w.Win32_Processor():
        list.append(processor.Name.strip())
        # list.append("CPU型号: %s" % processor.Name.strip())
    for memModule in w.Win32_PhysicalMemory():
        totalMemSize=int(memModule.Capacity)
        neicun += "%.2fGB" %(totalMemSize / 1024 ** 3) +","
        # list.append("内存厂商: %s" %memModule.Manufacturer)
        # list.append("内存型号: %s" %memModule.PartNumber)
        # list.append("内存大小: %.2fGB" %(totalMemSize/1024**3))
    for disk in w.Win32_DiskDrive(InterfaceType = "IDE"):
        diskSize=int(disk.size)
        yingpan += "" + disk.Caption + " %.2fGB" %(diskSize/1024**3) + ","
        # list.append("磁盘名称: %s" %disk.Caption)
        # list.append("磁盘大小: %.2fGB" %(diskSize/1024**3))
    for xk in w.Win32_VideoController():
        display += ""+xk.name + ","
        # list.append("显卡名称: %s" %xk.name)
    for address in w.Win32_NetworkAdapterConfiguration (IPEnabled=1):
        ip += "" + address.MACAddress + "," + address.IPAddress[0] + ","
        # list.append("IP地址: %s" % address.IPAddress[0])
        # list.append("MAC地址: %s" % address.MACAddress)
    for sys in w.Win32_OperatingSystem():
        name = sys.Caption
    name = ""+name + "[%s]"%platform.version()
    list.append(neicun)
    list.append(name)
    list.append(yingpan)
    list.append(display)
    list.append(ip)


def main():
    global path
    path= "c:/ccd"
    for BIOSs in w.Win32_ComputerSystem():
        UserNames=BIOSs.Caption
    fileName=path+os.path.sep+UserNames+".txt"
    info()
    http.request('POST', 'http://192.168.0.168:8000/login_action',
                 {"name": list[1], "name_yu": "", "computer_name": list[0], "c_cpu": list[4], "c_memory": list[5],
                  "c_network": list[9], "c_date": list[7], "system_type": list[6], "c_display": list[8]})

    #判断文件夹（路径）是否存在
    if not os.path.exists(path):
        print("不存在")
        #创建文件夹（文件路径）
        os.makedirs(path)
        #写入文件信息
        with open(fileName,'w+') as f:
            for li in list:
                print(li)
                l=li+"\n"
                f.write(l)
    else:
        print("存在")
        with open(fileName,'w+') as f:
            i = 0
            for li in list:
                print(li)
                print(i)
                l=li+"\n"
                f.write(l)
                i += 1

main()
